<?php
/**
 * Created by PhpStorm.
 * User: LiuJiaJun
 * Date: 2019/1/24
 * Time: 9:37
 */

namespace api\storage\controller;


use api\home\controller\RestController;
use api\storage\common\ClientFactory;

class ClientController extends RestController
{


    /**
     * @api {POST} /api/storage/client/log_file_upload 上传日志文件
     * @apiName  logFileUpload
     * @apiGroup  client
     * @apiVersion 0.0.1
     * @apiDescription 用于客户端上传日志
     *
     * @apiParam {string} type apk(pda端) | jar (socket客户端)
     * @apiParam {file} logFile 名字为logFile的文件
     * @apiParam {string} code 如果是type为apk的话，code=username。如果type为jar的话，code=uid
     *
     * @apiSuccess {int} code 0
     * @apiSuccess {string} msg 上传成功
     * @apiError  Error
     **/
    public function logFileUpload()
    {
        ini_set("upload_max_filesize", '64M');
        ini_set('post_max_size', '64M');

        $result = $this->validate($this->request->param(), 'Client.upload');
        if ($result !== true) {
            $this->error($result);
        }
        $type = input('type');
        $code = input('code');
        $file = $this->request->file('logFile');
        if (empty($file)) {
            $this->error('文件不存在');
        }
        $service = ClientFactory::getService($type);
        $result = $service->uploadLog($file, $code);
        switch ($result['status']) {
            case 1:
                $this->success('上传成功');
                break;
            case 0:
                $this->error('上传失败');
                break;
            default:
                $this->error('请求失败');
        }
    }

    /**
     * @api {GET} /api/storage/client/client_auth 客户端认证
     * @apiName  authClient
     * @apiGroup  client
     * @apiVersion 0.0.1
     * @apiDescription 用于socket客户端认证，客户端通过发送code进行认证
     *
     * @apiParam {string} code 分配好的code码
     *
     * @apiSuccess {int} code 0
     * @apiSuccess {string} msg 获取成功
     * @apiSuccess {object} data
     * @apiSuccess {string} data.uid 返回分配好的uid
     * @apiError  Error
     **/
    public function authClient()
    {
        $code = input('code');
        if (empty($code)) {
            $this->error('code为空');
        }
        $clientService = ClientFactory::getService('jar');
        $uid = $clientService->authClient($code);
        if (empty($uid)) {
            $this->error('获取失败');
        } else {
            $this->success('获取成功', ['uid' => $uid]);
        }
    }

    /**
     * @api {GET} /api/storage/client/version_check 新版本检测
     * @apiName  checkVersion
     * @apiGroup  client
     * @apiVersion 0.0.1
     * @apiDescription 检测是否有新版本，有多个版本的情况下取最新的版本
     *
     * @apiParam {string} type apk(pda端) | jar (socket客户端)
     * @apiParam {string} host 服务前的地址：如：192.168.168.1:81/iwarehouseX/public
     * @apiParam {string} version 可选，客户端的版本号。如果有该字段，会查询是否有高于版本的信息。主版本.次版本 如：1.21
     *
     * @apiSuccess {int} code 0
     * @apiSuccess {string} msg 获取成功
     * @apiSuccess {object} data
     * @apiSuccess {string} data.url 版本更新的url
     * @apiError  Error
     **/
    public function checkVersion()
    {
        $result = $this->validate($this->request->param(), 'Client.checkVersion');
        if ($result !== true) {
            $this->error($result);
        }
        $type = input('type');
        $version = input('version');
        $host = input('host');
        $service = ClientFactory::getService($type);
        $result = $service->checkVersion($version);
        switch ($result['status']) {
            case -1:
                $this->error('url为空');
                break;
            case 0:
                $this->success('没有最新版本');
                break;
            case 1:
                $data = $result['data'];
                $data['url'] = $host . $data['url'];
                $this->success('获取成功', $data);
                break;
            default:
                $this->error('请求失败');
        }
    }

    /**
     * @api {GET} /api/storage/client/history_version 获取历史版本
     * @apiName  historyVersion
     * @apiGroup  client
     * @apiVersion 0.0.1
     * @apiDescription 获取历史版本
     *
     * @apiParam {string} type apk(pda端) | jar (socket客户端)
     * @apiParam {string} host 服务前的地址：如：192.168.168.1:81/iwarehouseX/public
     *
     * @apiSuccess {int} code 0
     * @apiSuccess {string} msg 获取成功
     * @apiSuccess {object} data
     * @apiSuccess {string} data.url 下载地址
     * @apiError  Error
     **/
    public function historyVersion()
    {
        $type = input('type');
        $host = input('host');
        $service = ClientFactory::getService($type);
        $result = $service->historyVersion();
        switch ($result['status']) {
            case 0:
                $this->success('没有历史版本');
                break;
            case 1:
                $data = $result['data'];
                foreach ($data as &$item) {
                    $item['url'] = $host . $item['url'];
                }
                $this->success('获取成功', $data);
                break;
            default:
                $this->error('请求失败');
        }
    }
}